﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MyCollection
{
    class MySimpleCollection : ICollection
    {
        private readonly object __mutex = new object();
        private readonly MyKeyValuePair[] m_values = null;
        private readonly int m_count = 0;

        #region Constructor

        public MySimpleCollection(IEnumerable<MyKeyValuePair> values, int count)
        {
            Array.CreateInstance(typeof(MyKeyValuePair), count);
            
            int index = -1;
            foreach (var item in values)
            {
                index++;
                if (index > count)
                    break;

                m_values.SetValue(item, index);
            }

            m_count = index - 1; 
        }

        #endregion

        #region ICollection implemintation

        public void CopyTo(Array array, int index)
        {
            if (array == null)
                throw new ArgumentNullException();

            if (index < 0)
                throw new ArgumentOutOfRangeException();

            if (array.Length - index < m_count)
                throw new ArithmeticException("array");

            for (int i = 0; i < m_count - index; i++)
            {
                array.SetValue(m_values[i + index], i);
            }
        }

        public int Count
        {
            get { return m_count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return __mutex; }
        }

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
